package com.atguiju.preparedstatement.crud;

import com.atguiju.bean.Customer;
import com.atguiju.util.JDBCUtils;
import org.junit.Test;

import java.lang.reflect.Field;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.ArrayList;
import java.util.List;

public class PreparedStatementQueryTest {

    @Test
    public void queryTest() {
        String sql = "select id,name,email,birth from customers where id = ?";
        Customer instance = getInstance(Customer.class, sql, 1);
        System.out.println(instance);
    }

    @Test
    public void queryListTest() {
        String sql = "select id,name,email,birth from customers where id < ?";
        List<Customer> instanceList = getInstanceList(Customer.class, sql, 12);
        System.out.println(instanceList);

        String sql2 = "select id,name,email,birth from customers";
        List<Customer> instanceList2 = getInstanceList(Customer.class, sql2);
        System.out.println(instanceList2);
    }

    /**
     * 针对单条数据的对象的返回
     * @param clazz
     * @param sql
     * @param args
     * @param <T>
     * @return
     */
    public <T> T getInstance(Class<T> clazz, String sql, Object...args) {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet resultSet = null;

        try {
            //1.获取数据库连接
            conn = JDBCUtils.getConnection();

            //2.数据库预编译
            ps = conn.prepareStatement(sql);
            for (int i = 0; i < args.length; i++) {
                ps.setObject(i + 1, args[i]);
            }
            //3.获取到查询数据
            resultSet = ps.executeQuery();
            //获取数据集的元数据
            ResultSetMetaData metaData = resultSet.getMetaData();
            int columnCount = metaData.getColumnCount();
            if (resultSet.next()) {
//                Customer customer = new Customer();
                T t = clazz.newInstance();
                for (int i = 0; i < columnCount; i++) {
                    //获取列值
                    Object objectValue = resultSet.getObject(i + 1);
                    //获取每个列的列名getColumnName（）
                    //获取每个列的别名getColumnLabel()
                    String name = metaData.getColumnLabel(i + 1);
                    Field field = clazz.getDeclaredField(name);
                    field.setAccessible(true);
                    field.set(t, objectValue);
                }
                System.out.println(t);
                return t;
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            JDBCUtils.close(conn, ps, resultSet);
        }
    }

    /**
     * 针对多条数据的对象的返回
     * @param clazz
     * @param sql
     * @param args
     * @param <T>
     * @return
     */
    public <T> List<T> getInstanceList(Class<T> clazz, String sql, Object...args) {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet resultSet = null;

        try {
            //1.获取数据库连接
            conn = JDBCUtils.getConnection();

            //2.数据库预编译
            ps = conn.prepareStatement(sql);
            for (int i = 0; i < args.length; i++) {
                ps.setObject(i + 1, args[i]);
            }
            //3.获取到查询数据
            resultSet = ps.executeQuery();
            //获取数据集的元数据
            ResultSetMetaData metaData = resultSet.getMetaData();
            int columnCount = metaData.getColumnCount();
            List<T> tList = new ArrayList<>();
            while (resultSet.next()) {
//                Customer customer = new Customer();
                T t = clazz.newInstance();
                for (int i = 0; i < columnCount; i++) {
                    //获取列值
                    Object objectValue = resultSet.getObject(i + 1);
                    //获取每个列的列名getColumnName（）
                    //获取每个列的别名getColumnLabel()
                    String name = metaData.getColumnLabel(i + 1);
                    Field field = clazz.getDeclaredField(name);
                    field.setAccessible(true);
                    field.set(t, objectValue);
                }
                System.out.println(t);
                tList.add(t);

            }
            return tList;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            JDBCUtils.close(conn, ps, resultSet);
        }
    }
}
